/**
 * @file:          DataSampler.c
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2024.12.09
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2024.12.09,11:53:52
 */

/* Include Files **************************************************************/
#include "DataSampler.h"
#include "DebugPrint.h"

/* Global Variable Define *****************************************************/

/* Function Define ************************************************************/

void DataSampler_Init(struct DataSampler *self)
{
    self->Run = DataSampler_Run;
    self->UpdateParam = DataSampler_UpdateParam;
    self->PrintData = DataSampler_PrintData;

    self->UpdateParam(self);
}

uint16_t DataSampler_Run(struct DataSampler *self)
{
    /* io trigger */
    self->in_io_level = *self->in_io_bind;

    if (self->param_sample_mode == DataSampler_Mode_IOTrig_Rising)
    {
        /* io edge check */
        if (self->in_io_level == 1 && self->io_level_last == 0)
        {
            self->io_trig_start = 1;
            self->io_trig_count = 0;
        }
    }
    else if (self->param_sample_mode == DataSampler_Mode_IOTrig_Falling)
    {
        /* io edge check */
        if (self->in_io_level == 0 && self->io_level_last == 1)
        {
            self->io_trig_start = 1;
            self->io_trig_count = 0;
        }
    }
    self->io_level_last = self->in_io_level;

    /* io trig delay */
    if (self->io_trig_start == 1)
    {
        self->io_trig_start = 0;
        self->io_trig_running = 1;
    }
    if (self->io_trig_running == 1)
    {
        if (self->io_trig_count < self->param_io_trig_delay_count)
        {
            self->io_trig_count++;
        }
        else
        {
            self->in_start = 1;
            self->io_trig_running = 0;
        }
    }

    if (self->in_start == 1)
    {
        /* init */
        self->in_start = 0;
        self->running = 1;
        self->finished = 0;

        self->index = 0;
        self->count = 0;
    }
    if (self->running == 1)
    {
        if (self->index < self->param_data_length)
        {
            if (self->count % self->param_sample_interval == 0)
            {
                /* recorded data */
                switch (self->param_data_type)
                {
                case DataSampler_DataType_uint8:
                    ((uint8_t *)self->data_buffer)[self->index] = *(uint8_t *)self->in_data;
                    break;
                case DataSampler_DataType_int8:
                    ((int8_t *)self->data_buffer)[self->index] = *(int8_t *)self->in_data;
                    break;
                case DataSampler_DataType_uint16:
                    ((uint16_t *)self->data_buffer)[self->index] = *(uint16_t *)self->in_data;
                    break;
                case DataSampler_DataType_int16:
                    ((int16_t *)self->data_buffer)[self->index] = *(int16_t *)self->in_data;
                    break;
                case DataSampler_DataType_uint32:
                    ((uint32_t *)self->data_buffer)[self->index] = *(uint32_t *)self->in_data;
                    break;
                case DataSampler_DataType_int32:
                    ((int32_t *)self->data_buffer)[self->index] = *(int32_t *)self->in_data;
                    break;
                case DataSampler_DataType_float:
                    ((float *)self->data_buffer)[self->index] = *(float *)self->in_data;
                    break;
                case DataSampler_DataType_double:
                    ((double *)self->data_buffer)[self->index] = *(double *)self->in_data;
                    break;
                default:
                    break;
                }

                self->index++;
            }
            self->count++;
        }
        else
        {
            self->running = 0;
            self->finished = 1;

            /* print start */
            if (self->param_auto_print == 1)
            {
                self->in_print_start = 1;
            }
        }
    }

    return self->finished;
}

void DataSampler_UpdateParam(struct DataSampler *self)
{
    /* init data */
    self->in_start = 0;
    self->in_print_start = 0;
    self->in_io_level = *self->in_io_bind;
    self->io_level_last = *self->in_io_bind;
    self->io_trig_start = 0;
    self->io_trig_running = 0;
    self->io_trig_count = 0;

    /* 申请动态内存 */
    switch (self->param_data_type)
    {
    case DataSampler_DataType_uint8:
        self->data_buffer = (uint8_t *)malloc(self->param_data_length * sizeof(uint8_t));
        break;
    case DataSampler_DataType_int8:
        self->data_buffer = (int8_t *)malloc(self->param_data_length * sizeof(int8_t));
        break;
    case DataSampler_DataType_uint16:
        self->data_buffer = (uint16_t *)malloc(self->param_data_length * sizeof(uint16_t));
        break;
    case DataSampler_DataType_int16:
        self->data_buffer = (int16_t *)malloc(self->param_data_length * sizeof(int16_t));
        break;
    case DataSampler_DataType_uint32:
        self->data_buffer = (uint32_t *)malloc(self->param_data_length * sizeof(uint32_t));
        break;
    case DataSampler_DataType_int32:
        self->data_buffer = (int32_t *)malloc(self->param_data_length * sizeof(int32_t));
        break;
    case DataSampler_DataType_float:
        self->data_buffer = (float *)malloc(self->param_data_length * sizeof(float));
        break;
    case DataSampler_DataType_double:
        self->data_buffer = (double *)malloc(self->param_data_length * sizeof(double));
        break;
    default:
        break;
    }
}

uint16_t DataSampler_PrintData(struct DataSampler *self)
{
    if (self->in_print_start == 1)
    {
        /* init */
        self->in_print_start = 0;
        self->print_running = 1;
        self->print_finished = 0;

        self->index = 0;
        self->count = 0;
        // DBprintfDMAAdv_Add("data:\r\n");
    }
    else if (self->print_running == 1)
    {
        if (self->index < self->param_data_length)
        {
            if (self->count % self->param_print_interval == 0)
            {
                /* print data */
                switch (self->param_data_type)
                {
                case DataSampler_DataType_uint8:
                    DBprintfDMAAdv_Add("%u", ((uint8_t *)self->data_buffer)[self->index]);
                    break;
                case DataSampler_DataType_int8:
                    DBprintfDMAAdv_Add("%d", ((int8_t *)self->data_buffer)[self->index]);
                    break;
                case DataSampler_DataType_uint16:
                    DBprintfDMAAdv_Add("%u", ((uint16_t *)self->data_buffer)[self->index]);
                    break;
                case DataSampler_DataType_int16:
                    DBprintfDMAAdv_Add("%d", ((int16_t *)self->data_buffer)[self->index]);
                    break;
                case DataSampler_DataType_uint32:
                    DBprintfDMAAdv_Add("%lu", ((uint32_t *)self->data_buffer)[self->index]);
                    break;
                case DataSampler_DataType_int32:
                    DBprintfDMAAdv_Add("%ld", ((int32_t *)self->data_buffer)[self->index]);
                    break;
                case DataSampler_DataType_float:

                    DBprintfDMAAdv_Add("%s", float2str(((float *)self->data_buffer)[self->index], 6));
                    break;
                case DataSampler_DataType_double:

                    break;
                default:
                    break;
                }
            DBprintfDMAAdv_AddStr("\r\n");
                self->index++;
            }
            self->count++;
        }
        else
        {
            // DBprintfDMAAdv_AddStr("------\r\n");
            self->print_running = 0;
            self->print_finished = 1;
        }
    }

    return self->print_finished;
}